package adapter;

import java.util.ArrayList;

import lfm.mpr.R;
import listener.CheckBoxClickListenerAlineacion;
import objects.Jugador;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class ExpandibleAdapterJugadores extends BaseExpandableListAdapter {

    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }

    private Context context;
    private ArrayList<String> groups;
    private ArrayList<ArrayList<Jugador>> children;

    public ExpandibleAdapterJugadores(Context context, ArrayList<String> groups,
            ArrayList<ArrayList<Jugador>> children) {
        this.context = context;
        this.groups = groups;
        this.children = children;
        Log.i("LFM_Log","Construcor ExpandibleAdapterJugadores");
    }

    /**
     * A general add method, that allows you to add a Vehicle to this list
     * 
     * Depending on if the category opf the vehicle is present or not,
     * the corresponding item will either be added to an existing group if it 
     * exists, else the group will be created and then the item will be added
     * @param vehicle
     */
    public void addItem(Jugador jugador) {
    	Log.i("LFM_Log","addItem ExpandibleAdapterJugadores");
        if (!groups.contains(jugador.getPosicion())) {
            groups.add(jugador.getPosicion());
        }
        int index = groups.indexOf(jugador.getPosicion());
        if (children.size() < index + 1) {
            children.add(new ArrayList<Jugador>());
        }
        children.get(index).add(jugador);
        Log.i("LFM_Log","addItem ExpandibleAdapterJugadores");
    }

    public Object getChild(int groupPosition, int childPosition) {
        return children.get(groupPosition).get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    // Return a child view. You can load your custom layout here.
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
    	Log.i("LFM_Log","getChildView ExpandibleAdapterJugadores");
    	Jugador jugador = (Jugador) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.rowbuttonlayout, null);
            
        }
        
        Log.i("LFM_Log","Recuperando el layout");
        TextView nombre = (TextView) convertView.findViewById(R.id.text_lst_name);
		//TextView codigo = (TextView) row.findViewById(R.id.text_lst_codigo);
		TextView equipo = (TextView) convertView.findViewById(R.id.text_lst_team);
		TextView precio = (TextView) convertView.findViewById(R.id.text_lst_price);
		if (nombre != null) nombre.setText(jugador.getNombre());
		//if (codigo != null) codigo.setText(String.valueOf(jugador.getCodigo())+"-");
		if (precio != null) precio.setText(String.valueOf(jugador.getPrecio()));
		if (equipo != null) equipo.setText(jugador.getEquipo());
		Log.i("LFM_Log","getChildView ExpandibleAdapterJugadores");
		//CheckBox ch = (CheckBox) convertView.findViewById(R.id.check);
		//TextView text_view = (TextView) convertView.findViewById(R.id.label);
		
		//al evento on Click le asignamos nuestro propio evento OnClick
		//ch.setOnClickListener( 
		               //new CheckBoxClickListenerAlineacion(position,this.lista_alineaciones,context));
        // Depending upon the child type, set the imageTextView01
        /*tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        if (vehicle instanceof Car) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.car, 0, 0, 0);
        } else if (vehicle instanceof Bus) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bus, 0, 0, 0);
        } else if (vehicle instanceof Bike) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bike, 0, 0, 0);
        }*/
        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        return children.get(groupPosition).size();
    }

    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    public int getGroupCount() {
        return groups.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // Return a group view. You can load your custom layout here.
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
    	Log.i("LFM_Log","getGroupView ExpandibleAdapterJugadores");
        String group = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.grouplayout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.group_name);
        tv.setText(group);
        Log.i("LFM_Log","getGroupView ExpandibleAdapterJugadores");
        return convertView;
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }

}
